package bean;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import dao.DAOBusca;
import dao.PesrsitDAO;
import padroes.singleton.Singleton;
import pojo.Matricula;

@ManagedBean
@SessionScoped
public class MatriculaBean {
	
	private Matricula matricula = new Matricula();
	
	private DAOBusca<Matricula> daoMatriculas = new DAOBusca<Matricula>();
	private List<Matricula> matriculas = new ArrayList<Matricula>();
	private PesrsitDAO s = Singleton.getInstancia();

	public MatriculaBean(){
		
	}
	public Matricula getMatricula(){
		return matricula;
	}
	
	public List<Matricula> getMatriculas(){
		matriculas = daoMatriculas.busca(Matricula.class);
		return matriculas;
	}
	
	
	
	public void setMatricula(Matricula matricula){
		this.matricula = matricula;
	}
	public void salvar(){
//		if(matricula.getId){
			try {
				s.salvar(matricula);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			FacesContext ctx = FacesContext.getCurrentInstance();  
			ctx.addMessage(null, new FacesMessage("Matricula "+ matricula.getId()+" Inserida com sucesso")); 
//		}else{
//			s.getInstancia().editar(matricula);
//			FacesContext ctx = FacesContext.getCurrentInstance();  
//			ctx.addMessage(null, new FacesMessage("Matricula editada com sucesso")); 
//		
//		}
//		matricula = new Matricula();
		atualizar();
	}
	public void atualizar(){
		matriculas = daoMatriculas.busca(Matricula.class);
	}
}
